package com.example.demo6.service;

import com.example.demo6.dao.SuppliersDao;
import com.example.demo6.entity.Goods;
import com.example.demo6.entity.Suppliers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Supplier;

/**
 * @Author: Lizhi
 * @Date:Create：in 2023/12/8 17:16
 */
@Service
public class SuppliersService implements SuppliersDao {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public int add(Suppliers suppliers) {
        String sql = "insert into suppliers value(null,?,?,?)";
        Object[] args = {suppliers.getName(),suppliers.getAddr(),suppliers.getPhone()};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public int delete(Suppliers suppliers) {
        String sql = "delete from suppliers where Id=?";
        Object[] args = {suppliers.getId()};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public int updata(Suppliers suppliers) {
        String sql="update suppliers set name=?,addr=?,phone=? where Id=?";
        Object[] args={
                suppliers.getName(),suppliers.getAddr(),suppliers.getPhone(),suppliers.getId()
        };
        return jdbcTemplate.update(sql, args);
    }
    @Override
    public Suppliers findone(Suppliers suppliers) {
        String sql = "select * from suppliers where Id="+suppliers.getId().toString();
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Suppliers>(Suppliers.class));

    }

//    @Override
//    public Suppliers findone(Suppliers suppliers) {
//        return null;
//    }

    @Override
    public List<Suppliers> findAll() {
        String sql = "select * from suppliers";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Suppliers>(Suppliers.class ));
    }
}
